/*
 * Copyright 2009-2012 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.ibatis.executor.statement;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.ExecutorException;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

/**
 * 主要的改进在于，不需要反射，可以获取delegate（StatementHandler）和MappedStatement。
 * 
 * @author Clinton Begin
 * @author 尹雷
 */
public class RoutingStatementHandler implements StatementHandler {

    private final StatementHandler delegate;

    public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
                                   ResultHandler resultHandler, BoundSql boundSql) {

        switch (ms.getStatementType()) {
            case STATEMENT:
                delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
                break;
            case PREPARED:
                delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
                break;
            case CALLABLE:
                delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
                break;
            default:
                throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
        }

    }

    public Statement prepare(Connection connection) throws SQLException {
        return delegate.prepare(connection);
    }

    public void parameterize(Statement statement) throws SQLException {
        delegate.parameterize(statement);
    }

    public void batch(Statement statement) throws SQLException {
        delegate.batch(statement);
    }

    public int update(Statement statement) throws SQLException {
        return delegate.update(statement);
    }

    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
        return delegate.<E> query(statement, resultHandler);
    }

    public BoundSql getBoundSql() {
        return delegate.getBoundSql();
    }

    public ParameterHandler getParameterHandler() {
        return delegate.getParameterHandler();
    }

    /**
     * 获得底层的StatementHandler
     * @return StatementHandler
     * @author yinlei
     */
    public StatementHandler getDelegate() {
        return delegate;
    }

    /**
     * 获得MappedStatement
     * @return MappedStatement
     * @author yinlei
     */
    @Override
    public MappedStatement getMappedStatement() {
        return delegate.getMappedStatement();
    }
}
